package com.net.request

import android.net.Uri
import com.net.NetUtils
import okhttp3.Request

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/6/24
 */
class GetRequestBuilder : BaseRequestBuilder() {

    override fun build(): NetUtils {
        val build = Request.Builder()
        //请求方式
        build.get()
        //URL 参数
        build.url(generateUrl(url, params))
        //请求tag
        tag?.let {
            build.tag(tag)
        }
        //请求header
        headers?.let {
            build.headers(generateHeader(it))
        }

        return NetUtils(build.build())
    }

    /**
     * 拼接url
     */
    private fun generateUrl(url: String, params: MutableMap<String, String>?): String {
        if (params == null) {
            return url
        }
        val builder = Uri.parse(url).buildUpon()
        params.forEach { (key, value) ->
            builder.appendQueryParameter(key, value)
        }
        return builder.build().toString()
    }
}